import Axios from 'axios'

const state = {
	loading: [true],		//加载动画
	treeData: [],			//树形结构数据
	tableData: [],			//右侧表格数据
	expandNodeId: [],		//要展开的节点
	isCheckBetweenObject: [false],		//是否勾选往来科目
	betweenObject: [],		//往来对象表格数据
	node: [],				//当前点击的树形节点
	groupNode: null,		//当前科目组树形节点
	rowId: null,			//修改的会计科目id
	groupStatus: [],		//科目组操作状态 0 == 新增  1 == 编辑
	multipleSelection: [],
	selectRowId: [],
	groupTreeData: [],		//用户组树形结构
	searchResult: [],		//存储所有搜索结果
}

const mutations = {
	updateLoading(state, bool) {
		state.loading.splice(0, 1, bool);
	},
	updateTreeData(state, arr) {
		if(Object.prototype.toString.call(arr) !== "[object Array]") return;
		let treeData = state.treeData;
		treeData.splice(0, 1, ...arr);
		this.commit('accountSubjectStore/setCode', treeData);
	},
	updateTableData(state, arr) {		//更新表格数据
		if(Object.prototype.toString.call(arr) !== "[object Array]") return;
		let tableData = state.tableData;
		tableData.length = 0;
		arr.length > 0 ? tableData.splice(0, 1, ...arr) : tableData.splice(0);
	},
	setCode(state, arr) {		//加上编码
		for(let key in arr) {
			//7大类不显示编码
			if(arr[key]['code'].split('.')[0] !== '101') {
				arr[key]['name'] = arr[key]['code'] + ' (' + arr[key]['name'] + ')';
			}
			if(arr[key]['subAssertList'] && arr[key]['subAssertList'].length > 0) {
				this.commit('accountSubjectStore/setCode', arr[key]['subAssertList']);
			}
		}
	},
	updateExpandNodeId(state, id) {
		state.expandNodeId.splice(0, 1, id);
	},
	updateIsCheckBetweenObject(state, bool) {
		state.isCheckBetweenObject.splice(0, 1, bool);
	},
	updateBetweenObject(state, params) {			//保存往来对象表格数据
		let betweenObject = state.betweenObject;
		if(Object.prototype.toString.call(params) === '[object Object]') {
			//如果是新增修改时双击添加进来的
			if(betweenObject.length > 0) {
				//判断是否已经存在该条数据
				let flag = true;
				betweenObject.forEach(item => {
					if(item['id'] === params['id']) {
						flag = false;
						return false;
					}
				});
				if(flag) betweenObject.push(params);
			} else {
				betweenObject.push(params);
			}
		} else if(Object.prototype.toString.call(params) === '[object Array]') {
			betweenObject.length = 0;
			params.length > 0 ? betweenObject.splice(0, 1, ...params) : betweenObject.splice(0);
		}
	},
	deleteBetweenObjectById(state, id) {			//删除指定的往来对象数据，如果参数为空，默认删除数组第一条
		let betweenObject = state.betweenObject;
		if(betweenObject.length > 0) {
			if(id) {
				betweenObject.forEach((item, index) => {
					if(item['id'] === id) {
						betweenObject.splice(index, 1);
					}
				});
			} else {
				betweenObject.shift();
			}
		} else {
			betweenObject.splice(0);
		}
	},
	clearBetweenObject(state) {			//清空往来对象数据
		state.betweenObject.splice(0);
	},
	updateNode(state, node) {
		state.node.splice(0, 1, node);
	},
	updateGroupNode(state, node) {
		node ? state.groupNode = node : state.groupNode = null;
	},
	updateRowId(state, id) {
		id ? state.rowId = id : state.rowId = null;
	},
	updateGroupStatus(state, n) {
		state.groupStatus.splice(0, 1, n);
	},
	multipleSelection(state, arr) {
        state.multipleSelection.length = 0;
        state.selectRowId.length = 0;
        if(arr.length > 0) {
            arr.forEach(item => {
                state.multipleSelection.push(item);
                state.selectRowId.push(item['id']);
            });
        } else {
            state.multipleSelection.splice(0);
            state.selectRowId.splice(0);
        }
    },
    updateGroupTreeData(state, arr) {
    	if(Object.prototype.toString.call(arr) !== "[object Array]") return;
		let groupTreeData = state.groupTreeData;
		groupTreeData.splice(0, 1, ...arr);
		this.commit('accountSubjectStore/setCode', groupTreeData);
    },
    updateSearchResult(state, arr) {
    	if(Object.prototype.toString.call(arr) !== "[object Array]") return;
    	let searchResult = state.searchResult;
    	searchResult.length = 0;
    	arr.length > 0 ? searchResult.splice(0, 1, ...arr) : searchResult.splice(0);
    }
}

const actions = {
	getTreeData({state, commit, dispatch, rootState}) {		//获取树形结构数据
		commit('updateLoading', true);
		Axios.get('subjectGroup/selectGroupByObject?systemId=' + rootState.systemId).then(res => {
			if(res.data.code == 20001) {
				commit('updateLoading', false);
				if(res.data.data) {
					let data = res.data.data;
					commit('updateTreeData', data);
					commit('updateExpandNodeId', data[0]['id']);
					dispatch('getSearchData');		//同时调用拼音搜索接口
				}
			} else {
				commit('updateLoading', false);
			}
		});
	},
	getSubjectTableDataById({commit}, id) {		//通过会计科目id找到表格数据
		Axios.get('subjectGroup/selectSubjectByObject?id=' + id).then(res => {
			if(res.data.code == 20001) {
				if(res.data.data) {
					commit('updateTableData', res.data.data);
				}
			}
		});
	},
	getGroupTreeData({state, commit, rootState}) {
		commit('updateLoading', true);
		Axios.get('subjectGroup/selectGroupByObject?systemId=' + rootState.systemId).then(res => {
			if(res.data.code == 20001) {
				commit('updateLoading', false);
				if(res.data.data) {
					let data = res.data.data;
					commit('updateGroupTreeData', data);
				}
			}
		});
	},
	getSearchData({commit}) {			//获取拼音搜索结果
		Axios.get('subjectGroup/likeName').then(res => {
			if(res.data.code == 20001) {
				if(res.data.data) {
					commit('updateSearchResult', res.data.data);
				}
			}
		});
	}
}


export default {
	namespaced: true,
	state,
	mutations,
	actions
}